package com.stx.bms.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.stx.bms.dao.UserDAO;
import com.stx.bms.dao.UserDAOImpl;
import com.stx.bms.entity.ResultObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkUser")
public class UserCheckServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");

        UserDAO dao = new UserDAOImpl();
        boolean isExist = dao.checkUsername(username);

        ResultObject obj = null;
        if(isExist){
            obj = new ResultObject(true, "用户名已存在！");

        }else{
            obj = new ResultObject(false, "");
        }

        //obj用JSON送回到网页
        ObjectMapper mapper = new ObjectMapper();
        String str = mapper.writeValueAsString(obj);

        response.getWriter().write(str);
        response.getWriter().close();

    }
}
